package com.sjft.cloud.commons.security.config;

import com.sjft.cloud.commons.core.constant.Constant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.oauth2.provider.token.store.redis.RedisTokenStore;
import org.springframework.util.Assert;

/**
 * TokenStoreConfig
 *
 * @author Jin
 */
@Configuration
public class TokenStoreConfig {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @RefreshScope
    @Bean
    public RedisTokenStore getRedisTokenStore() {
        Assert.state(redisTemplate != null, "RedisTemplate must be provided");
        RedisTokenStore redisTokenStore = new RedisTokenStore(redisTemplate.getConnectionFactory());
        redisTokenStore.setPrefix(Constant.PREFIX + Constant.OAUTH_PREFIX);
        return redisTokenStore;
    }

}
